package com.sudoku.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.sudoku.observer.Event;
import com.sudoku.observer.Subject;
import com.sudoku.utility.Constants;
import com.sudoku.utility.Locator;

public class DifficultyScreen implements Constants{

	private MenuScreen menu;
	private Skin skin;
	private TextureAtlas atlas;
	private TextButtonStyle textButtonStyle, playStyle, exitStyle;
	private TextButton buttonEasy, buttonHard, buttonExtreme, buttonBack;
	private Subject subject;

	public DifficultyScreen(MenuScreen screen, Stage stage, BitmapFont textFont, Subject subject) {
		menu = screen;
		this.subject=subject;
		atlas = new TextureAtlas(Gdx.files.internal("data/button.pack"));
		skin = new Skin(atlas);

		textButtonStyle = new TextButtonStyle();
		textButtonStyle.up = skin.getDrawable("button.up");
		textButtonStyle.down = skin.getDrawable("button.down");
		textButtonStyle.pressedOffsetX = 1;
		textButtonStyle.pressedOffsetY = -1;
		textButtonStyle.font = textFont;
		playStyle = new TextButtonStyle(textButtonStyle);
		playStyle.fontColor = new Color(0.5F, 0.5F, 0.5F, 1f);
		exitStyle = new TextButtonStyle(textButtonStyle);
		exitStyle.fontColor = new Color(0.5F, 0.5F, 0.5F, 1f);

		createDifficultyButtons();
		
		buttonEasy.setPosition(ACTUAL_VIRTUAL_WIDTH * 0.1f, ACTUAL_VIRTUAL_HEIGHT * MenuScreen.HEADING_HEIGHT);
		buttonHard.setPosition(ACTUAL_VIRTUAL_WIDTH * 0.1f, ACTUAL_VIRTUAL_HEIGHT * (MenuScreen.HEADING_HEIGHT-0.16f));
		buttonExtreme.setPosition(ACTUAL_VIRTUAL_WIDTH * 0.1f, ACTUAL_VIRTUAL_HEIGHT * (MenuScreen.HEADING_HEIGHT-0.32f));
		buttonBack.setPosition(ACTUAL_VIRTUAL_WIDTH * 0.1f, ACTUAL_VIRTUAL_HEIGHT * (MenuScreen.HEADING_HEIGHT-0.54f));
		
		stage.addActor(buttonEasy);
		stage.addActor(buttonHard);
		stage.addActor(buttonExtreme);
		stage.addActor(buttonBack);
	}

	private void createDifficultyButtons() {
		// create buttons
		buttonEasy = new TextButton("Easy", playStyle);
		buttonEasy.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				subject.notify(Event.EVENT_CLICK);
				Locator.setDifficulty(DIFFICULTY.EASY);
				gotoMenuScreen();
				menu.startGame();
			}
		});
		buttonEasy.pad(10.0f);
		
		buttonHard = new TextButton("Hard", exitStyle);
		buttonHard.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				subject.notify(Event.EVENT_CLICK);
				Locator.setDifficulty(DIFFICULTY.HARD);
				gotoMenuScreen();
				menu.startGame();
			}
		});
		buttonHard.pad(10.0f);
		
		buttonExtreme = new TextButton("Extreme", exitStyle);
		buttonExtreme.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				subject.notify(Event.EVENT_CLICK);
				Locator.setDifficulty(DIFFICULTY.EXTREME);
				gotoMenuScreen();
				menu.startGame();
			}
		});
		buttonExtreme.pad(10.0f);
		
		buttonBack = new TextButton("Back", exitStyle);
		buttonBack.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				subject.notify(Event.EVENT_CLICK);
				gotoMenuScreen();
			}
		});
		buttonBack.pad(10.0f);

	}

	public void gotoMenuScreen() {
		hideButtons();
	}

	public void hideButtons() {
		buttonEasy.setVisible(false);
		buttonEasy.setDisabled(true);
		buttonHard.setVisible(false);
		buttonHard.setDisabled(true);
		buttonExtreme.setVisible(false);
		buttonExtreme.setDisabled(true);
		buttonBack.setVisible(false);
		buttonBack.setDisabled(true);
	}

	public void showButtons() {
		buttonEasy.setVisible(true);
		buttonEasy.setDisabled(false);
		buttonHard.setVisible(true);
		buttonHard.setDisabled(false);
		buttonExtreme.setVisible(true);
		buttonExtreme.setDisabled(false);
		buttonBack.setVisible(true);
		buttonBack.setDisabled(false);
	}
}
